﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using AmazonGlacierGUI.Core;
using AmazonGlacierGUI.Core.Helpers;
using AmazonGlacierGUI.Core.Model;
using AmazonGlacierGUI.UI.Infrastructure;
using AmazonGlacierGUI.UI.Infrastructure.MVVMFramework;
using GalaSoft.MvvmLight.Command;

namespace AmazonGlacierGUI.UI.ViewModels
{
    //19-sep-2012 Priyan R
    public class SettingsViewModel : AppViewModelBase
    {
        public SettingsViewModel()
        {
            if (AppContext.Current.Settings != null)
            {
                AccessKey = AppContext.Current.Settings.AccessKey;
                SecretAccessKey = AppContext.Current.Settings.SecretAccessKey;
            }
            WireCommnds();
        }
        private void OnOkCommand()
        {
            if(!ValidateAll()) return;
            if (AppContext.Current.Settings != null)
            {
                AppContext.Current.Settings.AccessKey = AccessKey;
                AppContext.Current.Settings.SecretAccessKey = SecretAccessKey;
                AppContext.Current.GetServiceFactory().AppService.SaveAppSettings();
            }
            else
            {
                var appsettings = new AppSettings();
                appsettings.AccessKey = AccessKey;
                appsettings.SecretAccessKey = SecretAccessKey;
                AppContext.Current.GetServiceFactory().AppService.SaveAppSettings(appsettings);
            }
            Close();
        }
        void WireCommnds()
        {
            OkCommand=new RelayCommand(OnOkCommand);
        }       
        public RelayCommand OkCommand { get; set; }
        private string _secretAccessKey;
        [Required]
        public string SecretAccessKey
        {
            get { return _secretAccessKey; }
            set
            {
                if (_secretAccessKey != value)
                {
                    _secretAccessKey = value;                    
                    RaisePropertyChanged(() => SecretAccessKey);
                }
            }
        }
        private string _accessKey;
        [Required]
        public string AccessKey
        {
            get { return _accessKey; }
            set
            {
                if (_accessKey != value)
                {
                    _accessKey = value;                    
                    RaisePropertyChanged(() => AccessKey);
                }
            }
        }
    }
}
